package com.fangliai.core.context;


import com.fangliai.core.entitiy.SystemModelEnum;
import com.fangliai.core.exception.BaseException;
import com.fangliai.core.util.StringHelper;
import com.fangliai.core.constants.CommonConstants;

import java.util.HashMap;
import java.util.Map;

/**
 * 后台当前上下文
 */
public class BaseContextHandler {
	public static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<Map<String, Object>>();

	public static void set(String key, Object value) {
		Map<String, Object> map = threadLocal.get();
		if (map == null) {
			map = new HashMap<String, Object>();
			threadLocal.set(map);
		}
		map.put(key, value);
	}

	public static Object get(String key) {
		Map<String, Object> map = threadLocal.get();
		if (map == null) {
			map = new HashMap<String, Object>();
			threadLocal.set(map);
		}
		return map.get(key);
	}


    /**
     *
     * @return
     */
    public static String getSysModl(){
        Object value = get(CommonConstants.CONTEXT_KEY_SYS_MODEL);
        return returnObjectValue(value).toUpperCase();
    }

    public static void setSysModl(String mod){
        set(CommonConstants.CONTEXT_KEY_SYS_MODEL,mod);
    }


    /**
     * 获取当前登录所承认商/公司ID
     * @return
     * @throws BaseException
     */
    public static String getCysIdOrCommpanyId() throws BaseException {
        SystemModelEnum modelEnum=null;
        try {
            modelEnum=SystemModelEnum.valueOf(getSysModl());
        }catch (Exception e){
                throw  new BaseException("登录域错误,当前所在域:"+e.getMessage());
        }
        switch (modelEnum){
            case CYS:
                DRIVER:
                 return getCysID();
            case USER:
                return  getCommpanyId();
            default:
               return  null;
        }
    }
    /**
     * 获取承运商ID
     * @return
     */
	public static String getCysID(){
		Object value = get(CommonConstants.CONTEXT_KEY_CYS_ID);
		return returnObjectValue(value);
	}

    /**
     * 获取公司id
     * @return
     */
	public static String getCommpanyId(){
        Object value = get(CommonConstants.CONIEXT_KEY_COMMPANY_ID);
        return returnObjectValue(value);
    }
    public static void setCommpanyIdID(String commpanyId){
        set(CommonConstants.CONIEXT_KEY_COMMPANY_ID, commpanyId);
    }
	public static void setCysID(String cysID){
		set(CommonConstants.CONTEXT_KEY_CYS_ID, cysID);
	}

	public static String getCarDriverId(){
		Object value = get(CommonConstants.CONTEXT_KEY_CAR_CARDRIVER_ID);
		return returnObjectValue(value) ;
	}

	public static void setCarDriverId(String carDriverId){
		set(CommonConstants.CONTEXT_KEY_CAR_CARDRIVER_ID,carDriverId) ;
	}

	public static String getUserID() {
		Object value = get(CommonConstants.CONTEXT_KEY_USER_ID);
		return returnObjectValue(value);
	}

	public static String getUsername() {
		Object value = get(CommonConstants.CONTEXT_KEY_USERNAME);
		return returnObjectValue(value);
	}


	public static String getName() {
		Object value = get(CommonConstants.CONTEXT_KEY_USER_NAME);
		return StringHelper.getObjectValue(value);
	}

	public static String getToken() {
		Object value = get(CommonConstants.CONTEXT_KEY_USER_TOKEN);
		return StringHelper.getObjectValue(value);
	}

	public static void setToken(String token) {
		set(CommonConstants.CONTEXT_KEY_USER_TOKEN, token);
	}

	public static void setName(String name) {
		set(CommonConstants.CONTEXT_KEY_USER_NAME, name);
	}

	public static void setUserID(String userID) {
		set(CommonConstants.CONTEXT_KEY_USER_ID, userID);
	}

	public static void setUsername(String username) {
		set(CommonConstants.CONTEXT_KEY_USERNAME, username);
	}

    private static String returnObjectValue(Object value) {
        return value == null ? null : value.toString();
    }

	public static void remove() {
		threadLocal.remove();
	}
}
